<?php
namespace app\gym\controller;
use app\common\controller\AdminBase;
/*案例*/
class GymOrderHistory extends AdminBase{

	function _initialize() {
    }

    function index(){
    	$auth_group=sp_get_auth_menu_3("10051003");
		$this->assign("menus",$auth_group);
    	echo $this->fetch();
    }
	
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	/*编辑*/
    function edit(){
    	$shop_id=sp_get_current_shop_id();
		$id=input("id");
		$history=db("GymOrderHistory")->fetchsql(FALSE)->where("id=$id and shop_id=$shop_id")->find();
		$member=db("GymMember")->where("shop_id=$shop_id and id=".$history['gym_member_id'])->find();
    	$this->assign("history",$history);
		$this->assign("member",$member);
		$this->assign("list",$this->get_change($history,$member));
    	echo $this->fetch();
    }
	//获取合同变化前和变化后的数据
	private function get_change($history,$member){
		if($history['opt_type']=="购卡"){
			$list[]=array("name"=>"卡号","new"=>$history['card_no']);
			$list[]=array("name"=>"卡种名称","new"=>$history['gym_card_name']);
			$list[]=array("name"=>"有效期开始","new"=>$history['start_date_desc']);
			$list[]=array("name"=>"有效期结束","new"=>$history['end_date_desc']);
			$list[]=$this->get_card_type($history);
			$list[]=array("name"=>"备注","new"=>$history['descs']);
		}
		if($history['opt_type']=="升级"){
			$list[]=array("name"=>"有效期开始","new"=>$history['start_date_desc']);
			$list[]=array("name"=>"有效期结束","new"=>$history['end_date_desc']);
			$list[]=$this->get_card_type($history);
			$list[]=array("name"=>"备注","new"=>$history['descs']);
		}
		return $list;
	}

	private function get_card_type($history){
		if(in_array($history['opt_type'], array("购卡","升级"))){
			if($history['gym_card_type']==1){//1次卡
				return array("name"=>"可用次数","new"=>$history['able_count_desc']);
			}
			if($history['gym_card_type']==2){//2 期限卡
				return array("name"=>"可用天数","new"=>$history['able_day_desc']);
			}
			if($history['gym_card_type']==3){//3可用金额
				return array("name"=>"可用金额","new"=>$history['able_money_desc']);
			}
			return;
		}
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
		 'name'=>array("field" =>"a.name","operator" =>"="),
		 'mobile'=>array("field" =>"a.mobile","operator" =>"="),
		 'card_no'=>array("field" =>"b.card_no","operator" =>"="),
		 'opt_type'=>array("field" =>"b.opt_type","operator" =>"="),
		);
		$shop_id=sp_get_current_shop_id();
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and a.shop_id=$shop_id";
		$total=db("GymOrderHistory")->alias("b")->join([['m_gym_member a',"a.id=b.gym_member_id and a.shop_id=$shop_id","LEFT"],['m_admin c',"b.admin_id=c.id and c.shop_id=$shop_id","LEFT"]])->where($where_and)->fetchsql(FALSE)->count(1);
		$field="b.id,b.opt_type,b.card_no,b.create_time,a.name,a.mobile,c.account,b.descs";
		$list=db("GymOrderHistory")->alias("b")->field($field)->join([['m_gym_member a',"a.id=b.gym_member_id and a.shop_id=$shop_id","LEFT"],['m_admin c',"b.admin_id=c.id and c.shop_id=$shop_id","LEFT"]])->where($where_and)->order($this->get_order("b"))->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}

	//新增购卡记录
	function add_history_1($order,$order_old=""){
		$admin_id=sp_get_current_admin_id();
		$shop_id=sp_get_current_shop_id();
		db("GymOrderHistory")->insert(array(
		    "opt_type"=>"购卡",
			"order_no"=>$order['order_no'],
			"card_no"=>$order['card_no'],
			"shop_id"=>$shop_id,
			"gym_member_id"=>$order['gym_member_id'],
			"admin_id"=>$admin_id,
			"gym_card_name"=>$order['gym_card_name'],
			"gym_card_type"=>$order['gym_card_type'],
			"gym_card_id"=>$order['gym_card_id'],
			"pay_money"=>$order['pay_money'],
			"pay_type"=>$order['pay_type'],
			"order_date"=>$order['order_date'],
			"member_ship_id"=>$order['member_ship_id'],
			"descs"=>$order['descs'],
			"gift"=>$order['gift'],
			"start_date_desc"=>$order["start_date"],
			"end_date_desc"=>$order["end_date"],
			"able_count_desc"=>$order["able_count"],
			"able_money_desc"=>$order["able_money"],
			"able_day_desc"=>$order["able_day"],
			"order_id"=>$order["order_id"],
		));
	}
	//新增升级记录 
	function add_history_2($order,$order_old){
		$admin_id=sp_get_current_admin_id();
		$shop_id=sp_get_current_shop_id();
		$data=array(
		    "opt_type"=>"升级",
			"order_no"=>$order['order_no'],
			"card_no"=>$order_old['card_no'],
			"shop_id"=>$shop_id,
			"gym_member_id"=>$order_old['gym_member_id'],
			"admin_id"=>$admin_id,
			"gym_card_name"=>$order_old['gym_card_name'],
			"gym_card_type"=>$order_old['gym_card_type'],
			"pay_money"=>$order['pay_money'],
			"pay_type"=>$order['pay_type'],
			"order_date"=>$order['order_date'],
			"member_ship_id"=>$order_old['member_ship_id'],
			"descs"=>$order['descs'],
			"start_date_desc"=>$order_old["start_date"]."--".$order_old["start_date"],
			"end_date_desc"=>$order_old["end_date"]."--".$order["end_date"],
			"order_id"=>$order_old["id"],
		);
		if($order_old['gym_card_type']==2){//期限卡
			$data["able_day_desc"]=$this->_get_able_day($order_old['start_date'],$order_old['end_date'])."--".$order['able_day'];
		}else if($order_old['gym_card_type']==1){//次卡
			$data["able_count_desc"]=$order_old["able_count"]."--".$order['able_count'];
		}else if($order_old['gym_card_type']==3){//储值卡
			$data["able_money_desc"]=$order_old["able_money"]."--".$order['able_money'];			
		}
		db("GymOrderHistory")->insert($data);
	}
	function _get_able_day($start,$end){
	   if(strcmp($start,$end)<0){
	   	   if(strcmp(date("Y-m-d"),$start)<0){//未开卡
	   	   		$day=(strtotime("$end")-strtotime("$start"))/(24*60*60); 
	   	   }else{
	   	   	   $day=(strtotime("$end")-strtotime(date("Y-m-d")))/(24*60*60);
	   	   }
	   }else{
	   	  $day=0;
	   }	
	   return $day;
	}
	//新增续卡记录
	function add_history_3($order,$order_old){
		$admin_id=sp_get_current_admin_id();
		$shop_id=sp_get_current_shop_id();
		$data=array(
		    "opt_type"=>"续卡",
			"order_no"=>$order['order_no'],
			"card_no"=>$order_old['card_no'],
			"shop_id"=>$shop_id,
			"gym_member_id"=>$order_old['gym_member_id'],
			"admin_id"=>$admin_id,
			"gym_card_name"=>$order['gym_card_name'],
			"gym_card_id"=>$order['gym_card_id'],
			"gym_card_type"=>$order_old['gym_card_type'],
			"pay_money"=>$order['pay_money'],
			"pay_type"=>$order['pay_type'],
			"order_date"=>$order['order_date'],
			"member_ship_id"=>$order['member_ship_id'],
			"descs"=>$order['descs'],
			"start_date_desc"=>$order_old["start_date"],
			"end_date_desc"=>$order_old["end_date"]."--".$order["end_date"],
			"order_id"=>$order_old["id"],
		);
		if($order_old['gym_card_type']==2){//期限卡
			$data["able_day_desc"]=$this->_get_able_day($order_old['start_date'],$order['end_date']);
		}else if($order_old['gym_card_type']==1){//次卡
			$data["able_count_desc"]=$order_old["able_count"]."--".$order['able_count'];
		}else if($order_old['gym_card_type']==3){//储值卡
			$data["able_money_desc"]=$order_old["able_money"]."--".$order['able_money'];			
		}
		db("GymOrderHistory")->insert($data);
	
	}
	//新增退款重办记录
	function add_history_4($order,$order_old){
		$admin_id=sp_get_current_admin_id();
		$shop_id=sp_get_current_shop_id();
		db("GymOrderHistory")->insert(array(
		    "opt_type"=>"退款重办",
			"order_no"=>$order['order_no'],
			"card_no"=>$order['card_no'],
			"shop_id"=>$shop_id,
			"gym_member_id"=>$order['gym_member_id'],
			"admin_id"=>$admin_id,
			"gym_card_name"=>$order['gym_card_name'],
			"gym_card_type"=>$order['gym_card_type'],
			"gym_card_id"=>$order['gym_card_id'],
			"pay_money"=>(0-$order['pay_money']),
			"pay_type"=>$order['pay_type'],
			"order_date"=>$order['order_date'],
			"member_ship_id"=>$order['member_ship_id'],
			"descs"=>$order['descs'],
			"gift"=>$order['gift'],
			"start_date_desc"=>$order["start_date"],
			"end_date_desc"=>$order["end_date"],
			"able_count_desc"=>$order["able_count"],
			"able_money_desc"=>$order["able_money"],
			"able_day_desc"=>$order["able_day"],
			"order_id"=>$order["id"],
		));
	}
	//新增退会记录
	function add_history_5($order,$order_old){
		$admin_id=sp_get_current_admin_id();
		$shop_id=sp_get_current_shop_id();
		db("GymOrderHistory")->insert(array(
		    "opt_type"=>"退会",
			"order_no"=>$order_old['order_no'],
			"card_no"=>$order['card_no'],
			"shop_id"=>$shop_id,
			"gym_member_id"=>$order['gym_member_id'],
			"admin_id"=>$admin_id,
			"gym_card_name"=>$order['gym_card_name'],
			"gym_card_type"=>$order['gym_card_type'],
			"gym_card_id"=>$order['gym_card_id'],
			"pay_money"=>(0-$order_old['pay_money']),
			"pay_type"=>$order_old['pay_type'],
			"order_date"=>$order_old['order_date'],
			"member_ship_id"=>$order['member_ship_id'],
			"descs"=>$order_old['descs'],
			"gift"=>$order['gift'],
			"start_date_desc"=>$order["start_date"],
			"end_date_desc"=>$order["end_date"],
			"able_count_desc"=>$order["able_count"],
			"able_money_desc"=>$order["able_money"],
			"able_day_desc"=>$order_old["able_day"],
			"order_id"=>$order["id"],
		));
	}
	//新增换卡记录
	function add_history_6($order,$order_old){
		$admin_id=sp_get_current_admin_id();
		$shop_id=sp_get_current_shop_id();
		db("GymOrderHistory")->insert(array(
		    "opt_type"=>"换卡",
			"order_no"=>$order_old['order_no'],
			"card_no"=>$order_old['card_no'],
			"shop_id"=>$shop_id,
			"gym_member_id"=>$order['gym_member_id'],
			"admin_id"=>$admin_id,
			"gym_card_name"=>$order['gym_card_name'],
			"gym_card_type"=>$order['gym_card_type'],
			"gym_card_id"=>$order['gym_card_id'],
			"pay_money"=>$order_old['pay_money'],
			"pay_type"=>$order_old['pay_type'],
			"order_date"=>$order_old['order_date'],
			"member_ship_id"=>$order['member_ship_id'],
			"descs"=>$order_old['descs'],
			"gift"=>$order['gift'],
			"start_date_desc"=>$order["start_date"],
			"end_date_desc"=>$order["end_date"],
			"able_count_desc"=>$order["able_count"],
			"able_money_desc"=>$order["able_money"],
			"able_day_desc"=>$order["able_day"],
			"order_id"=>$order["id"],
		));
	}
	//新增转卡记录
	function add_history_7($order,$order_old){
		$admin_id=sp_get_current_admin_id();
		$shop_id=sp_get_current_shop_id();
		
		db("GymOrderHistory")->insert(array(
		    "opt_type"=>"换卡(转出)",
			"order_no"=>$order_old['order_no'],
			"card_no"=>$order['card_no'],
			"shop_id"=>$shop_id,
			"gym_member_id"=>$order['gym_member_id'],
			"admin_id"=>$admin_id,
			"gym_card_name"=>$order['gym_card_name'],
			"gym_card_type"=>$order['gym_card_type'],
			"gym_card_id"=>$order['gym_card_id'],
			"pay_money"=>$order_old['pay_money'],
			"pay_type"=>$order_old['pay_type'],
			"order_date"=>$order_old['order_date'],
			"member_ship_id"=>$order['member_ship_id'],
			"descs"=>$order_old['descs'],
			"gift"=>$order['gift'],
			"start_date_desc"=>$order["start_date"],
			"end_date_desc"=>$order["end_date"],
			"able_count_desc"=>$order["able_count"],
			"able_money_desc"=>$order["able_money"],
			"able_day_desc"=>$order["able_day"],
			"order_id"=>$order["id"],
		));
		
		db("GymOrderHistory")->insert(array(
		    "opt_type"=>"换卡(转入)",
			"order_no"=>time().rand(100,999),
			"card_no"=>$order['card_no'],
			"shop_id"=>$shop_id,
			"gym_member_id"=>$order_old['gym_member_id'],
			"admin_id"=>$admin_id,
			"gym_card_name"=>$order['gym_card_name'],
			"gym_card_type"=>$order['gym_card_type'],
			"gym_card_id"=>$order['gym_card_id'],
			"pay_money"=>$order_old['pay_money'],
			"pay_type"=>$order_old['pay_type'],
			"order_date"=>$order_old['order_date'],
			"member_ship_id"=>$order['member_ship_id'],
			"descs"=>$order_old['descs'],
			"gift"=>$order['gift'],
			"start_date_desc"=>$order["start_date"],
			"end_date_desc"=>$order["end_date"],
			"able_count_desc"=>$order["able_count"],
			"able_money_desc"=>$order["able_money"],
			"able_day_desc"=>$order["able_day"],
			"order_id"=>$order["id"],
		));
	}
	//新增请假记录
	function add_history_8($order,$order_old){
		
		$admin_id=sp_get_current_admin_id();
		$shop_id=sp_get_current_shop_id();
		
		db("GymOrderHistory")->insert(array(
		    "opt_type"=>"请假",
			"order_no"=>$order_old['order_no'],
			"card_no"=>$order['card_no'],
			"shop_id"=>$shop_id,
			"gym_member_id"=>$order['gym_member_id'],
			"admin_id"=>$admin_id,
			"gym_card_name"=>$order['gym_card_name'],
			"gym_card_type"=>$order['gym_card_type'],
			"gym_card_id"=>$order['gym_card_id'],
			"pay_money"=>$order_old['pay_money'],
			"pay_type"=>$order_old['pay_type'],
			"order_date"=>$order_old['order_date'],
			"member_ship_id"=>$order['member_ship_id'],
			"descs"=>$order_old['descs'],
			"gift"=>$order['gift'],
			"start_date_desc"=>$order["start_date"],
			"end_date_desc"=>$order_old["end_date"],
			"able_count_desc"=>$order["able_count"],
			"able_money_desc"=>$order["able_money"],
			"able_day_desc"=>$order["able_day"],
			"order_id"=>$order["id"],
		));
	}
	//新增销假记录
	function add_history_9($order,$order_old){
		
		
		$admin_id=sp_get_current_admin_id();
		$shop_id=sp_get_current_shop_id();
		
		db("GymOrderHistory")->insert(array(
		    "opt_type"=>"销假",
			"order_no"=>$order_old['order_no'],
			"card_no"=>$order['card_no'],
			"shop_id"=>$shop_id,
			"gym_member_id"=>$order['gym_member_id'],
			"admin_id"=>$admin_id,
			"gym_card_name"=>$order['gym_card_name'],
			"gym_card_type"=>$order['gym_card_type'],
			"gym_card_id"=>$order['gym_card_id'],
			"pay_money"=>0,
			"pay_type"=>"其他",
			"order_date"=>date("Y-m-d"),
			"member_ship_id"=>$order['member_ship_id'],
			"descs"=>$order_old['descs'],
			"gift"=>$order['gift'],
			"start_date_desc"=>$order["start_date"],
			"end_date_desc"=>$order["end_date"],
			"able_count_desc"=>$order["able_count"],
			"able_money_desc"=>$order["able_money"],
			"able_day_desc"=>$order["able_day"],
			"order_id"=>$order["id"],
		));
	
	}
	
	
	///////////////////////////////////////////////////////////////////////////////////
}
